﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using AutoLoL.Commands;
using AutoLoL.Entities;
using AutoLoL.Logic.Masteries;
using MessageBox = System.Windows.Forms.MessageBox;
using Path = System.Windows.Shapes.Path;
using AutoLoL.Logic.Factories;

namespace AutoLoL.Dialogs
{
    /// <summary>
    /// Interaction logic for ImportMasteriesWindow.xaml
    /// </summary>
    public partial class ImportLolmDialog : Window
    {
        public ImportMasteriesViewModel ViewModel = new ImportMasteriesViewModel();

        public ImportLolmDialog()
        {
            InitializeComponent();
            ViewModel.ParentWindow = this;
            this.DataContext = ViewModel;
        }

    }

    public class ImportMasteriesViewModel : DependencyObject
    {
        public Window ParentWindow { get; set; }

        public string SelectedDirectory
        {
            get { return (string)GetValue(SelectedDirectoryProperty); }
            set { SetValue(SelectedDirectoryProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectedDirectory.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedDirectoryProperty = DependencyProperty.Register("SelectedDirectory", typeof(string), typeof(ImportMasteriesViewModel));

        public ObservableCollection<LolmListItem> LolmFiles { get; set; }

        #region commands
        private ICommand _checkAllCommand;
        public ICommand CheckAllCommand
        {
            get
            {
                if (_checkAllCommand == null)
                    _checkAllCommand = new RelayCommand(CheckAll, () => LolmFiles.Count > 0);
                return _checkAllCommand;
            }
        }

        private ICommand _unCheckAllCommand;
        public ICommand UnCheckAllCommand
        {
            get
            {
                if (_unCheckAllCommand == null)
                    _unCheckAllCommand = new RelayCommand(UnCheckAll, () => LolmFiles.Count > 0);
                return _unCheckAllCommand;
            }
        }

        private ICommand _importCommand;
        public ICommand ImportCommand
        {
            get
            {
                if (_importCommand == null)
                    _importCommand = new RelayCommand(Import, () => LolmFiles.FirstOrDefault(x => x.IsChecked) != null);
                return _importCommand;
            }
        }

        private ICommand _browseCommand;
        public ICommand BrowseCommand
        {
            get
            {
                if (_browseCommand == null)
                    _browseCommand = new RelayCommand(SelectFolder);
                return _browseCommand;
            }
        }

        private ICommand _changeChampionCommand;
        public ICommand ChangeChampionCommand
        {
            get
            {
                if (_changeChampionCommand == null)
                    _changeChampionCommand = new RelayCommandWithParam(ChangeChampion);
                return _changeChampionCommand;
            }
        }

        #endregion

        public ImportMasteriesViewModel()
        {
            LolmFiles = new ObservableCollection<LolmListItem>();
        }

        private void ChangeChampion(object sender)
        {
            LolmListItem listItem = (sender as LolmListItem);
            ChampionDialog champDialog = new ChampionDialog(ParentWindow, Logic.Factories.ChampionFactory.Instance.GetAll().ToList());
            champDialog.ShowDialog();

            if (listItem != null && champDialog.SelectedChampions != null)
            {
                listItem.ChampionBuild.Champion = champDialog.SelectedChampions.FirstOrDefault();
                listItem.UpdateBindings();
            }

        }

        private void Import()
        {
            foreach (LolmListItem lolmListItem in LolmFiles.Where(f => f.IsChecked))
            {
                Logic.ChampionBuildRepository.Save(lolmListItem.ChampionBuild);
                lolmListItem.Imported = true;
            }
            new ConfirmDialog(ParentWindow, LolmFiles.Where(f => f.Imported).Count() + " builds were successfully imported.", "Import Successful", false).ShowDialog();
            if (ParentWindow != null)
            {
                ParentWindow.Close();
            }
        }

        private void CheckAll()
        {
            foreach (LolmListItem lolmListItem in LolmFiles)
            {
                lolmListItem.IsChecked = true;
            }
        }

        private void UnCheckAll()
        {
            foreach (LolmListItem lolmListItem in LolmFiles)
            {
                lolmListItem.IsChecked = false;
            }
        }

        private void SelectFolder()
        {
            //SelectedDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            System.Windows.Forms.FolderBrowserDialog folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
            folderBrowserDialog.ShowNewFolderButton = false;
            folderBrowserDialog.Description = "Select your old /Saves directory.";
            folderBrowserDialog.SelectedPath = SelectedDirectory;
            System.Windows.Forms.DialogResult result = folderBrowserDialog.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                LolmFiles.Clear();
                SelectedDirectory = folderBrowserDialog.SelectedPath;

                var lolmFiles = Directory.GetFiles(SelectedDirectory, "*.lolm");
                foreach (string lolmPath in lolmFiles)
                {
                    string path = lolmPath;

                    IEnumerable<ChampionBuild> builds = LolmMasteryImporter.Import(lolmPath, ChampionFactory.Instance, MasteryFactory.Instance);
                    foreach (ChampionBuild build in builds)
                    {
                        LolmFiles.Add(new LolmListItem(path, build));
                    }
                }

                if (LolmFiles.Count == 0)
                {
                    new ConfirmDialog(ParentWindow, "There are no *.lolm files in this directory. Please select your old /Saves directory.", "No files found", false).ShowDialog();
                }
            }
        }

        public class LolmListItem : DependencyObject, INotifyPropertyChanged
        {

            public bool Imported { get; set; }

            public bool IsChecked
            {
                get { return (bool)GetValue(IsCheckedProperty); }
                set { SetValue(IsCheckedProperty, value); }
            }

            // Using a DependencyProperty as the backing store for IsChecked.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty IsCheckedProperty =
                DependencyProperty.Register("IsChecked", typeof(bool), typeof(LolmListItem));

            public ChampionBuild ChampionBuild
            {
                get { return (ChampionBuild)GetValue(ChampionBuildProperty); }
                set { SetValue(ChampionBuildProperty, value); }
            }

            // Using a DependencyProperty as the backing store for ChampionBuild.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty ChampionBuildProperty =
                DependencyProperty.Register("ChampionBuild", typeof(ChampionBuild), typeof(LolmListItem));

            public string Path { get; private set; }
            public string Name { get { return System.IO.Path.GetFileNameWithoutExtension(Path); } }

            public LolmListItem(string lolmPath, ChampionBuild champBuild)
            {
                Path = lolmPath;
                ChampionBuild = champBuild;

            }

            internal void UpdateBindings()
            {
                NotifyPropertyChanged("ChampionBuild");
            }

            public event PropertyChangedEventHandler PropertyChanged;

            public void NotifyPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
    }
}
